package com.example.springboot.controller;

import com.example.springboot.common.Result;
import com.example.springboot.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 购物车表(Cart)表控制层
 *
 * @author makejava
 * @since 2024-01-13 17:16:25
 */
@RestController
@RequestMapping("/cart")
public class CartController {
    /**
     * 服务对象
     */
    @Autowired
    private CartService cartService;


    @PostMapping("/getCarts")
    public Result getCarts(@RequestParam("currentPage") Integer currentPage,
                           @RequestParam("pageSize") Integer pageSize,
                           @RequestParam("userId") Integer userId) {
        return Result.success(cartService.getCarts(currentPage, pageSize, userId));
    }


    @PostMapping("/deleteByIds")
    public Result deleteByIds(@RequestBody List<Integer> ids) {
        cartService.deleteByIds(ids);
        return Result.success("删除成功");
    }

    @PostMapping("/deleteById")
    public Result deleteById(@RequestParam("id") Integer id) {
        cartService.deleteById(id);
        return Result.success("删除成功");
    }

    @PostMapping("/updateCartNumAndTotalPrice")
    public Result updateCartNumAndTotalPrice(@RequestParam("id") Integer id,
                                             @RequestParam("quantity") Integer quantity,
                                             @RequestParam("total") Integer total) {
        cartService.updateCartNumAndTotalPrice(id, quantity, total);
        return Result.success("更新成功");
    }


    @PostMapping("/getCartProductsByIds")
    public Result getCartProductsByIds(@RequestBody List<Integer> ids) {
        return Result.success(cartService.getCartProductsByIds(ids));
    }


    @PostMapping("/selectByIds")
    public Result selectByIds(@RequestBody List<Integer> ids) {
        return Result.success(cartService.selectByIds(ids));
    }


    @PostMapping("/add")
    public Result add(@RequestBody Cart cart) {
        cartService.add(cart);
        return Result.success("添加成功");
    }


    @PostMapping("/createCart")
    public Result createCart(@RequestBody Cart cart) {
        return Result.success(cartService.createCart(cart));
    }
}

